package com.lixueju.security.box.validate.code;

import com.lixueju.security.box.core.properties.SecurityBoxProperties;
import com.lixueju.security.box.validate.code.image.ImageValidateCodeGenerator;
import com.lixueju.security.box.validate.code.sms.DefaultSmsValidateCodeSender;
import com.lixueju.security.box.validate.code.sms.SmsValidateCodeSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * <p>
 * 验证码Bean的配置类
 * </p>
 *
 * @author lixueju
 * @since 2019-09-07
 */
@Configuration
public class ValidateCodeBeanConfig {

    @Autowired
    private SecurityBoxProperties securityBoxProperties;

    /**
     * 图片验证码图片生成器
     * 注解@ConditionalOnMissingBean表示，在执行之前会先在容器中查找name为imageCodeGenerator的Bean，如果找到了就不会执行了。
     */
    @Bean
    @ConditionalOnMissingBean(name = "imageValidateCodeGenerator")
    public ValidateCodeGenerator imageValidateCodeGenerator() {
        ImageValidateCodeGenerator codeGenerator = new ImageValidateCodeGenerator();
        codeGenerator.setSecurityBoxProperties(securityBoxProperties);
        return codeGenerator;
    }

    /**
     * 短信验证码发送器
     * 注解@ConditionalOnMissingBean表示，在执行之前会先在容器中查找类型为SmsValidateCodeSender的Bean，如果找到了就不会执行了。
     */
    @Bean
    @ConditionalOnMissingBean(SmsValidateCodeSender.class)
    public SmsValidateCodeSender smsCodeSender() {
        return new DefaultSmsValidateCodeSender();
    }
}
